<template>
  <div>
    <BasicTable
      @register="registerTable"
      @resize-column="handleResizeColumn"
      :canResize="false"
      :scroll="{ x: 'max-content' }"
    >
      <template #toolbar>
        <a-button type="error" @click="handleDelete"> 批量删除 </a-button>
        <a-button type="success" @click="handleCharge"> 充值 </a-button>
        <a-button type="error" @click="handleRefund"> 退款 </a-button>
        <a-button type="success" @click="handleCreate">添加企业</a-button>
        <a-button type="success" @click="handleEdit">编辑企业</a-button>
        <a-button type="primary" @click="manageCarTeam">管理车队</a-button>
        <a-button type="primary" @click="manageCar">管理车辆</a-button>
        <a-button type="primary" @click="manageAdmin">管理员管理</a-button>
      </template>
    </BasicTable>
    <RechargeModal @register="registerCharge" @success="handleSuccess" />
    <RefundModal @register="registerRefund" @success="handleSuccess" />
    <CompanyModal @register="registerCompany" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
  name: 'ChargeManager',
});
</script>
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { BasicTable, useTable } from '@/components/Table';
import { useModal } from '@/components/Modal';
import RechargeModal from './components/RechargeModal.vue';
import RefundModal from './components/RefundModal.vue';
import CompanyModal from './components/CompanyModal.vue';
import { columns, searchFormSchema } from './data';
import { useMessage } from '@/hooks/web/useMessage';
import { useGo } from '@/hooks/web/usePage';
import { defHttp } from '@/utils/http/axios';
import { downloadBlob } from '@/utils/file/download';
import { PageEnum } from '@/enums/pageEnum';
const { createMessage } = useMessage();
const go = useGo();
const [registerCharge, { openModal: openChargeModal }] = useModal();
const [registerRefund, { openModal: openRefundModal }] = useModal();
const [registerCompany, { openModal: openCompanyModal }] = useModal();
const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] =
  useTable({
    //title: '列表',
    api: '/admin/company/list',
    columns,
    showIndexColumn: true,
    useSearchForm: true,
    indexColumnProps: {
      fixed: 'left',
    },
    formConfig: {
      labelWidth: 70,
      hideLabel: true,
      schemas: searchFormSchema,
      baseColProps: { span: 6 },
    },
    pagination: true,
    bordered: true,
    // actionColumn: {
    //   width: 50,
    //   title: '操作',
    //   dataIndex: 'action',
    //   slots: { customRender: 'action' },
    //   fixed: 'right',
    // },
    rowKey: 'id',
    rowSelection: {
      type: 'checkbox',
    },
  });
const exportLoading = ref(false);
onMounted(() => {});

function handleCharge() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行编辑');
  } else {
    openChargeModal(true, {
      isUpdate: false,
      companyId: getSelectRows()[0].id,
    });
  }
}
function handleCreate() {
  openCompanyModal(true, {
    isUpdate: false,
  });
}
function handleEdit() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行编辑');
  } else {
    openCompanyModal(true, {
      isUpdate: true,
      companyId: getSelectRows()[0].id,
    });
  }
}
function handleRefund() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行编辑');
  } else {
    openRefundModal(true, {
      isUpdate: false,
      companyId: getSelectRows()[0].id,
    });
  }
}
function manageCarTeam() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行车队管理');
  } else {
    go('/carteam/teamlist?companyId=' + getSelectRows()[0].id);
  }
}
function manageCar() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行车队管理');
  } else {
    go('/car/list?companyId=' + getSelectRows()[0].id);
  }
}
function manageAdmin() {
  if (getSelectRows().length === 0 || getSelectRows().length > 1) {
    createMessage.error('请选择一个企业进行管理员管理');
  } else {
    go('/operate/adminlist?companyId=' + getSelectRows()[0].id);
  }
}
async function handleDelete() {
  if (getSelectRows().length === 0) {
    createMessage.error('请选择一个企业');
  } else {
    const res = await defHttp.post({
      url: `/admin/company/delete`,
      data: getSelectRowKeys(),
    });
    createMessage.success(res.message);
    reload();
  }
}

function handleSuccess() {
  reload();
}
</script>
